﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * Original source PushButton Engine:
 * Copyright (C) 2009 PushButton Labs, LLC
 * For more information see http://www.pushbuttonengine.com
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using Microsoft.Xna.Framework;
using Box2D.XNA;

namespace AAEngine.Box2D
{
    public class CircleCollisionShape : CollisionShape
    {
        private float _radius = 1;
        private Vector2 _offset = Vector2.Zero;

        public float Radius
        {
            get { return _radius; }
            set
            {
                _radius = value;

                if (_parent != null)
                {
                    _parent.BuildCollisionShapes();
                }
            }
        }

        public Vector2 Offset
        {
            get { return _offset; }
            set
            {
                _offset = value;

                if (_parent != null)
                {
                    _parent.BuildCollisionShapes();
                }
            }
        }

        protected override FixtureDef DoCreateFixture()
        {
            Vector2 halfSize = _parent.Size * 0.5f;
            float scale = _parent.SpatialManager.InverseScale;

            var fixture = base.DoCreateFixture();

            CircleShape shape = new CircleShape();
            shape._radius = _radius * scale * (halfSize.X > halfSize.Y ? halfSize.X : halfSize.Y);
            shape._p = _offset;

            fixture.shape = shape;

            return fixture;
        }
    }
}
